package com.jrx.anytxn.transaction.job.paph.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.PaphFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author 张靖烽
 * @name GenerateServiceFeeListener
 * @description 检查文件
 * @create 2021-03-13 10:53
 **/
@Service
public class GenerateServiceFeeListener implements StepExecutionListener {

    private static final Logger logger = LoggerFactory.getLogger(GenerateServiceFeeListener.class);


    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private PaphFileConfig paphFileConfig;

    @Override
    public void beforeStep(StepExecution stepExecution) {
        // 当前业务日期
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        String nextProcessingDate = DateFormatUtils.format(org.getNextProcessingDate(), DateUtils.DATE_PATTERN_ANOTHER);
        checkFileAndPath(org.getTenantId(), nextProcessingDate, Constant.PRODECUT_TYPE_XF);
        checkFileAndPath(org.getTenantId(), nextProcessingDate, Constant.PRODECUT_TYPE_JY);
    }

    private void checkFileAndPath(String tenantId, String nextProcessingDate, String product) {
        // 文件路径
        String filePath = paphFileConfig.getServiceFeePath() + tenantId + File.separator + nextProcessingDate + File.separator + product + File.separator;

        // 文件名
        String fileName = Constant.LS_BANK + product + "_" + nextProcessingDate + "_" + TransBizConstant.SERVICE_FEE_FILE_NAME + "_" + paphFileConfig.getFileType() + ".txt";

        Path path = Paths.get(filePath);
        Path pathName = Paths.get(filePath + fileName);
        // 创建本月文件
        logger.info("》》》》平安普惠服务费文件生成前检查《《《《path:{},name:{}", path, pathName);
        //判断文件夹是否存在
        if (!Files.isDirectory(path)) {
            try {
                Files.createDirectories(path);
            } catch (IOException e) {
                logger.error("创建目录失败", e);
            }
        }
        // 删除文件
        if (Files.exists(pathName)) {
            try {
                Files.delete(pathName);
            } catch (IOException e) {
                logger.error("文件删除失败", e);
            }
        }
        //创建文件
        try {
            Files.createFile(pathName);
        } catch (IOException e) {
            logger.error("创建文件失败", e);
        }
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }
}